package com.siecom.transport.handler;

import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import com.siecom.bean.IdentityInfoBean;
import com.siecom.constconfig.ConstDefine;
import com.siecom.constconfig.ErrorDefine;
import com.siecom.hostservice.MainActivity;
import com.siecom.module.DeviceCallback;
import com.siecom.module.DeviceInterface;
import com.siecom.proto.Message;
import com.siecom.tool.IDPicture;
import com.siecom.tool.LogUtil;
import com.siecom.transport.CommServer;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created on 2017/1/14.
 */

public class IdentityHandler implements ServiceHandler {
    private static final String TAG = IdentityHandler.class.getSimpleName();
    private CommServer server;
    @Override
    public void onReceive(CommServer server, Message.siecomproto msg,String message) {
        this.server = server;
        disposeMessage(msg,message);

    }
    String tempMsgId = "";
    public void disposeMessage(Message.siecomproto msg,String message){
        switch (msg.getCmd()){
            case ConstDefine.ID_READ_REQUEST:
                LogUtil.e(TAG,"进入读身份证");
                read(msg);
                break;
            case ConstDefine.IDENTITY_CANCEL_REQUEST:
                cancelId(msg);
                break;
        }

    }
    public void cancelId(Message.siecomproto msg){
        DeviceInterface.cancelIdentity();
        com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
        msgBuilder.setModule(msg.getModule());
        msgBuilder.setMsgid(msg.getMsgid());
        msgBuilder.setCmd(msg.getCmd() + 1);
        msgBuilder.setRespond("id cancel");
        msgBuilder.setError(0);
        com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
        server.sendToRemote(respondMsg);

        //取消后在原方法中返回
        if (!TextUtils.isEmpty(tempMsgId)){
            msgBuilder.setModule(msg.getModule());
            msgBuilder.setMsgid(tempMsgId);
            msgBuilder.setCmd(ConstDefine.ID_RESPOND);
            msgBuilder.setRespond("id cancel");
            msgBuilder.setError(ErrorDefine.ID_CANCEL_READ);
            com.siecom.proto.Message.siecomproto respondMsg2 = msgBuilder.build();
            server.sendToRemote(respondMsg2);
        }


    }
    public void read(Message.siecomproto msg){
        tempMsgId = msg.getMsgid();
        //设置超时时间最少为10秒
        int timeOut = Math.max(msg.getTimeout(), 10 * 1000);
//        timeOut = 10*1000;  //超时时间设置为10秒
        int orgData;
        try {
            JSONObject jsonObject = new JSONObject(msg.getRequest());
            orgData = jsonObject.optInt("orgData");
            LogUtil.e(TAG,"orgData == " + orgData);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        DeviceInterface.readIdentity(new DeviceCallback() {
            @Override
            public void onStart() {

                MainActivity.startIDAnime( timeOut);
            }
            @Override
            public void onError(Bundle bundle) {

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.ID_MODULE);
                msgBuilder.setCmd(ConstDefine.ID_RESPOND);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);

                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                LogUtil.e("身份证","onSuccess 读取成功");
                IdentityInfoBean bean = bundle.getParcelable("data");
                String orgText = bundle.getString("orgData");
                LogUtil.e(TAG,bean.toString());
                Gson gson = new Gson();
                String respond = gson.toJson(bean);

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.ID_MODULE);
                msgBuilder.setCmd(ConstDefine.ID_RESPOND);
                if (orgData == 1){
                    msgBuilder.setRequest(orgText);
                }

                if(MainActivity.connectMode == 1){ //BLE模式
                    if(bean.imageByte != null){
                        msgBuilder.setExtion(ByteString.copyFrom(bean.imageByte));
                    }
                    msgBuilder.setMsgid("");
                }else {
                    msgBuilder.setExtion(ByteString.copyFrom(bean.imageByte));
                    bean.setImageByte(null);
                    respond = gson.toJson(bean);
                    msgBuilder.setRespond(respond);
                }
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);
            }

            @Override
            public void onCompleted(Bundle bundle) {

                MainActivity.stopIDAnime();

            }
        }, timeOut, false);

    }

}
